<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>书本列表</title>
    <script crossorigin="anonymous" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
            src="https://lib.baomitu.com/jquery/1.12.4/jquery.min.js"></script>
    <style>
        #container {
            margin: auto;
            width: 620px;
            font-size: 12px;
        }

        table {
            border-right: solid 1px #000;
        }

        td {
            height: 30px;
        }

        .cell-head {
            border: solid 1px #000;
            border-right: 0;
            text-align: center;
            font-size: 16px;
        }

        .cell {
            border: solid 1px #000;
            border-top: 0;
            border-right: 0;
            text-align: center;
        }
    </style>
</head>
<body style="background-color: paleturquoise">

<div id="container">
    <div><a href="/view/books/add">添加书本</a></div>

    <table border="0" cellspacing="0" cellpadding="0" width="600">
        <caption style="font-size:30px;color: khaki">书本</caption>
        <tr class="row">
            <td class="cell-head">ID</td>
            <td class="cell-head">书名</td>
            <td class="cell-head">作者</td>
            <td class="cell-head">价格</td>
            <td class="cell-head">库存</td>
            <td class="cell-head">是否借阅</td>
            <td class="cell-head">操作</td>
        </tr>
        <tr class="row" th:each="book: ${books}">
            <td class="cell" th:text="${book.id}">Oranges</td>
            <td class="cell" th:text="${book.name}">Oranges</td>
            <td class="cell" th:text="${book.author}">Oranges</td>
            <td class="cell" th:text="${book.price}">Oranges</td>
            <td class="cell" th:text="${book.stock}">Oranges</td>
            <!--            <td class="cell" th:text="${book.borrowId!=null?'已借阅':'未借阅'}">是否借阅</td>-->
            <td class="cell" th:text="${book.statusDisplay}">Oranges</td>
            <td class="cell">
                <a th:href="${'/view/books/edit?id='+book.id}" class="GuanLi" th:if="${user.admin}">编辑</a>

                <a href="javascript:void(0)" class="delete" th:data-name="${book.name}" th:if="${user.admin}">删除</a>

                <a th:href="${'/view/books/stock?id='+book.id}" class="GuanLi" th:if="${user.admin}">设置库存</a>

                <a href="javascript:void(0)" class="JieShu" th:data-userId="${user.id}" th:data-bookId="${book.id}"
                   th:if="${book.status==null}">借书</a>

                <a href="javascript:void(0)" class="huanShu" th:data-userId="${user.id}" th:data-bookId="${book.id}"
                   th:if="${book.status==1}">还书</a>


                <a href="javascript:void(0)" class="TongGuo" th:data-borrowId="${book.borrowId}"
                   th:if="${book.status==0} and ${user.admin}" >通过</a>

                <a href="javascript:void(0)" class="JuJue" th:data-borrowId="${book.borrowId}"
                   th:if="${book.status==0} and ${user.admin} " >拒绝</a>
            </td>
        </tr>

    </table>

    <p><img src="/images/book.jpg" alt="book"></p>

</div>

<script type="text/javascript">
    $(document).ready(function () {
        $(".delete").click(function () {
            $.get('/book/delete', {
                name: $(this).attr('data-name')
            }, function (res) {
                if (res.code === 1) {
                    alert('删除成功');
                    location.href = "/view/books";
                } else {
                    alert(res.message);
                }
            }, 'json')
        });


        $(".JieShu").click(function () {
            $.get('/user/applyBorrow', {
                userId: $(this).attr('data-userId'),
                bookId: $(this).attr('data-bookId')
            }, function (res) {
                if (res.code === 1) {
                    alert("提交申请成功");
                    location.href = "/view/users";
                } else {
                    alert(res.message);
                }
            }, 'json')
        })

        $(".huanShu").click(function () {
            $.get('user/returnBooks', {
                userId: $(this).attr('data-userId'),
                bookId: $(this).attr('data-bookId')
            }, function (res) {
                if (res.code === 1) {
                    alert("还书成功");
                    location.href = "/view/users";
                } else {
                    alert(res.message);
                }
            }, 'json')
        })


        $(".TongGuo").click(function () {

            alert($(this).attr('data-borrowId'))

            $.get('/UserBookBorrow/audit', {
                id: $(this).attr('data-borrowId'),
                status: 1
            }, function (res) {
                if (res.code === 1) {
                    alert("申请通过");
                    location.href = "/view/users";
                } else {
                    alert(res.message);
                }
            }, 'json')
        })


        $(".JuJue").click(function () {
            $.get('/UserBookBorrow/audit', {
                id: $(this).attr('data-borrowId'),
                status: 2
            }, function (res) {
                if (res.code === 1) {
                    alert("申请拒绝");
                    location.href = "/view/users";
                } else {
                    alert(res.message);
                }
            }, 'json')
        })



    });


</script>

</body>
</html>